package com.fengdi.everyday.middle;

public class Bank {
    private final int accountMount;
    private final long[] balance;

    public Bank(long[] balance) {
        this.balance = balance;
        this.accountMount = this.balance.length;
    }

    public boolean transfer(int account1, int account2, long money) {
        if(!this.isLegalAccount(account1) || !this.isLegalAccount(account2)) return false;

        if(this.balance[account1 - 1] < money) return false;

        this.balance[account1 - 1] -= money;
        this.balance[account2 - 1] += money;
        return true;
    }

    public boolean deposit(int account, long money) {
        if(!this.isLegalAccount(account)) return false;
        this.balance[account - 1] += money;
        return true;
    }

    public boolean withdraw(int account, long money) {
        if(!this.isLegalAccount(account)) return false;

        if(this.balance[account - 1] < money) return false;

        this.balance[account - 1] -= money;
        return true;
    }

    private boolean isLegalAccount(int account) {
        return account > 0 && account <= this.accountMount;
    }
}
